package com.lenovo.manufacture.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class BlankFragment_seven extends Fragment {
    @BindView(R.id.lv)
    ListView lv;
    private List<Seven_list> seven_lists = new ArrayList<>();
//    private Handler handler=new Handler();
    public BlankFragment_seven() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_seven, container, false);
        ButterKnife.bind(this,view);
        initview();
//        handler.removeCallbacks(runnable);
//        handler.postDelayed(runnable,3000);
        return view;
    }
//    private Runnable runnable=new Runnable() {
//        @Override
//        public void run() {
//            seven_lists = new ArrayList<>();
//            initview();
//            handler.postDelayed(this,3000);
//        }
//    };
    public interface BlogSerive {
        @GET("getchargelog")
        Call<ResponseBody> Blog();
    }
    private void initview() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogSerive blogSerive = retrofit.create(BlogSerive.class);
        Call<ResponseBody> call = blogSerive.Blog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                String s = null;
                try {
                    s = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Seven_list carlist = gson.fromJson(s, Seven_list.class);
                seven_lists.add(carlist);
                lv.setAdapter(new Myadapter());
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return seven_lists.get(0).getData().size();
        }
        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = View.inflate(getContext(), R.layout.fragment_seven_one, null);
            ViewHolder viewHolder=new ViewHolder(view1);
            viewHolder.tvCz.setText(seven_lists.get(0).getData().get(i).getWho()+"");
            viewHolder.tvCzje.setText(seven_lists.get(0).getData().get(i).getCharge()+"");
            viewHolder.tvCarno.setText(seven_lists.get(0).getData().get(i).getCarno()+"");
            viewHolder.tvYe.setText(seven_lists.get(0).getData().get(i).getAmount()+"");
            viewHolder.tvCztime.setText(seven_lists.get(0).getData().get(i).getTime()+"");
            return view1;
        }
        class ViewHolder {
            @BindView(R.id.tv_cz)
            TextView tvCz;
            @BindView(R.id.tv_czje)
            TextView tvCzje;
            @BindView(R.id.tv_carno)
            TextView tvCarno;
            @BindView(R.id.tv_ye)
            TextView tvYe;
            @BindView(R.id.tv_cztime)
            TextView tvCztime;
            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
